<?php
/**
*
* ShopWindow Toolset
*
* Copyright (C) 2008 Digital Window Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*
*/

require_once(HOME_PATH.'classes'.DIRECTORY_SEPARATOR.'class.shopcore.php');
require_once(HOME_PATH.'classes'.DIRECTORY_SEPARATOR.'class.smarty_sw.php');


// init ShopCore
if ( !is_object($oShopCore) ) {
	$oShopCore = new shopcore();
}

// init Smarty
if ( !is_object($oSmarty) ) {
	$oSmarty= new smarty_sw();
}

// Expand all branches //
$oCategoryParentParams->bExpandAllBranches = true;

// load env vars into params obj
$oShopCore->syncApiParams($oCategoryParentParams);

# get CategoryTree
$oCategoryTree = $oShopCore->oApiCategoryTree->getCategoryTree($oCategoryParentParams);

// New array of categorys to return
$aCategoryArray = array();

// Remove any parent that is NOT in the category being displayed
foreach ($oCategoryTree->aChildren as $oCategory) {
	if ($oCategory->iId != $oCategoryTree->iActiveNode) {
		continue;
	}
	else {
		$aCategoryArray[] = $oCategory;
	}
}

// Update children with new array
$oCategoryTree->aChildren = $aCategoryArray;

// Quick check to make sure that our tree has children
$hasChildren = false;
foreach ($aCategoryArray as $oChild) {
    if (isset($oChild->oCategoryTree)) {
        $hasChildren = true;
        break;
    }
}

// If tree has no children then dont display category tree and redirect to productlist
if (!$hasChildren) {
    // Check our category ID is set else default to product list with no refine by
    if (isset($aCategoryArray[0]->iId) && is_numeric($aCategoryArray[0]->iId) && 1==2) {
        header("Location: ". $oShopCore->buildLink(P_PRODUCTLIST, '', '', $aCategoryArray[0]->iId));
    }
    else {
        header("Location: ". $oShopCore->buildLink(P_PRODUCTLIST));
    }
}

unset($aCategoryArray);

# SMARTY ASSIGN
$oSmarty->assign('oParentCategoryTree', 		$oCategoryTree);

?>